mod fib_circle;
mod fndemo;
mod chats;
mod print_args;
mod err_handle_demo;
mod pkg_scale_err_demo;
mod app_scale_err_demo;

use chats::chat::process_event;
use chats::chat::Event;
use chats::chat::Gender;
use chats::chat::Topic;
use chats::chat::TopicId;
use chats::chat::User;
use chats::chat::UserId;
use chats::chat::PI;
use chats::chat::USER0;
use chats::hello::say_hello;
use fib_circle::fib_for;
use fib_circle::fib_loop;
use fib_circle::while_loop;
use app_scale_err_demo::fetch_id;
use std::fs;
use sub;
use std::any::Any;

fn pi() -> f64 {
    3.1415926
}
fn not_pi() {
    3.1415926;
}
// fn apply(value: i32,f: fn(i32) -> i32)->i32{
//     f(value)
// }
// fn square(v:i32)->i32{
//     v*v
// }
// fn cube(v:i32)->i32{
//     v*v*v
// }

fn main() -> Result<(), Box<dyn std::error::Error>> {

    for arg in std::env::args(){
        println!("arg: {}",arg)
    }
    let mut x = "aa";
    println!("{}", x);
    x = "bb";
    println!("{}", x);
    println!("{}", PI);
    println!("{}", USER0);
    /* let url = "https://www.rust-lang.org/";
    let url2 = "https://www.rust-lang.org/";
    let output = "rust.md";
    let output2 = "rust2.md";

    println!("Fetching url: {}", url);
    println!("Fetching url: {}", url2);
    let body = reqwest::blocking::get(url).unwrap().text().unwrap();
    // let body2 = reqwest::blocking::get(url2).unwrap().text().unwrap();
    println!("Converting html to markdown...");
    let md = html2md::parse_html(&body);
    let md2 = html2md::parse_html(&body2);
    fs::write(output, md.as_bytes()).unwrap();
    fs::write(output2,md2.as_bytes()).unwrap();
    println!("Converted markdown has been saved in {}.", output);
    println!("Converted markdown has been saved in2 {}.",output2);*/

    println!("apply square: {}", fndemo::apply(2, fndemo::square));
    println!("apply cube: {}", fndemo::apply(2, fndemo::cube));

    let is_pi = pi();
    let is_unit1 = not_pi();
    let is_unit2 = {
        pi();
    };
    println!(
        "is pi:{:?},is_unit1:{:?},is_unit2:{:?}",
        is_pi, is_unit1, is_unit2
    );

    let alice = User {
        id: UserId(1),
        name: "Alice".into(),
        gender: Gender::Female,
    };
    let bob = User {
        id: UserId(2),
        name: "Bob".into(),
        gender: Gender::Male,
    };
    let topic = Topic {
        id: TopicId(1),
        name: "rust".into(),
        owner: UserId(1),
    };
    let event1 = Event::Join((alice.id, topic.id));
    let event2 = Event::Join((bob.id, topic.id));
    let event3 = Event::Message((alice.id, topic.id, "Hello world!".into()));
    println!(
        "event1: {:?}, event2: {:?}, event3: {:?}",
        event1, event2, event3
    );

    // Test for loop
    // fib_loop(255u8);
    // while_loop(255u8);
    // fib_for(255u8);
    let arr = [1, 2, 3];
    assert_eq!(arr[..], [1, 2, 3]);
    // assert_eq!(arr[1..=2], [2]);
    // assert_eq!(arr[1..arr.len()], [1, 2]);

    let url = "https://www.rust-lang.org/";
    let output = "rust.md";
    println!("Fetching url: {}", url);
    // let body = reqwest::blocking::get(url)?.text()?;
    println!("Converting html to markdown...");
    // let md = html2md::parse_html(&body);
    // fs::write(output, md.as_bytes())?;
    // println!("Converted markdown has been saved in {}.", output);
    say_hello("philip");
    sub::workspace_use::test1();
    print_args::print();

    let cc="aaa";
    let dd=&cc;
    let eq= &cc == dd;
    println!("cc == dd {:?}",eq);
    println!("cc: {:?}",&cc);
    println!("dd: {:?}",dd);
    let fetch_id_rst=fetch_id()?;
    // println!("fetch_id {:?}",&fetch_id_rst);
    // println!("fetch_id {:?}",fetch_id_rst);
    //
    println!("after error： won't print ");

    Ok(())
}
#[cfg(test)]
mod tests{
    #[test]
    fn if_works(){
        assert_eq!(2+3,5);
    }
}